package com.lengxf.design.common;

/**
 * 代理主体角色
 * By Lengxf
 */
public class ProxySearcher implements Searcher {

    //动态
    private final RealSearcher searcher = new RealSearcher();

    private final AccessValidator validator = new AccessValidator();

    private final Logger logger = new Logger();

    /**
     * 在真实主体方法上进行 前置 后置 处理
     * By Lengxf
     */
    @Override
    public String doSearch(String userId, String keyword) {
        if (this.validate(userId)) {
            String result = searcher.doSearch(userId, keyword);
            this.log(userId);
            return result;
        }
        return null;
    }

    public boolean validate(String userId) {
        return validator.validate(userId);
    }

    public void log(String userId) {
        logger.log(userId);
    }

}
